package cn.com.godliu.gateway.filters;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * GatewayFilter均在yml里要申明，并在application里进行@bean注解
 */
@Slf4j
public class LocalFilter implements GatewayFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());
        log.info("LocalFilter：{}", time);

//        Map<String, String> uriVariables = ServerWebExchangeUtils.getUriTemplateVariables(exchange);
//        log.info(uriVariables.toString());
//        log.info(exchange.getRequest().toString());

        //给下游请求加个header--request是只读属性，不能更改
//        exchange.getRequest().getHeaders().set("extends","123456");
        //给请求返回加个header，这个可以修改
        exchange.getResponse().getHeaders().set("response-header", "99090");

        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -200; //越小越优先
    }
}
